package com.zx.tradeclient.ctrl.index2;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.zx.base.ctrl.BaseActivity;
import com.zx.base.http.HttpUtils;
import com.zx.base.http.HttpUtils.CallBack;
import com.zx.base.util.ActivityTaskManager;
import com.zx.tradeclient.R;
import com.zx.tradeclient.app.TradeApp;
import com.zx.tradeclient.ctrl.index5.LoginActivity;

public class SupplyToBuyActivity extends BaseActivity implements OnClickListener {
	/** Called when the activity is first created. */

	private TextView supply_order_product_name;
	private TextView supply_order_number;
	private TextView supply_order_price;

	private EditText supply_order_contact;
	private EditText supply_order_phone;
	private EditText supply_order_address;
	private EditText supply_order_pay;
	private EditText supply_order_remark;

	private ProgressDialog progressDialog;

	private TradeApp app;

	private String supplyId = "";
	private String supplyNum = "";
	private String supplyName = "";
	private String supplyPrice = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.supply_to_buy_info);

		if (!(ActivityTaskManager.getInstance().containsName("SupplyToBuyActivity"))) {
			ActivityTaskManager.getInstance().putActivity("SupplyToBuyActivity", this);
		}

		init();

	}

	private void init() {

		setHeader(0);

		app = (TradeApp) getMyApplication();

		supplyId = getIntent().getStringExtra("supplyId");
		supplyNum = getIntent().getStringExtra("supplyNum");
		supplyName = getIntent().getStringExtra("supplyName");
		supplyPrice = getIntent().getStringExtra("supplyPrice");

		header_right.setText("订单提交");
		header_right.setVisibility(View.VISIBLE);
		header_right.setOnClickListener(this);

		supply_order_product_name = (TextView) findViewById(R.id.supply_order_product_name);
		supply_order_product_name.setText(supplyName);

		supply_order_number = (TextView) findViewById(R.id.supply_order_number);
		supply_order_number.setText(supplyNum);

		supply_order_price = (TextView) findViewById(R.id.supply_order_price);
		supply_order_price.setText(supplyPrice);

		supply_order_contact = (EditText) findViewById(R.id.supply_order_contact);
		supply_order_phone = (EditText) findViewById(R.id.supply_order_phone);
		supply_order_address = (EditText) findViewById(R.id.supply_order_address);
		supply_order_pay = (EditText) findViewById(R.id.supply_order_pay);
		supply_order_remark = (EditText) findViewById(R.id.supply_order_remark);

	}

	private void postSupplyOrderInfo() {

		Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
		map.put("act", "supplyOrderForm");
		map.put("supplyId", supplyId);

		map.put("buyerId", app.user.getId());
		map.put("num", supplyNum);
		map.put("buyerLinkMan", supply_order_contact.getText().toString());
		map.put("buyerLinkMobile", supply_order_phone.getText().toString());
		map.put("address", supply_order_address.getText().toString());
		map.put("tradeWay", "402881e93be49a32013be4b8d2410002");
		map.put("remark", supply_order_remark.getText().toString());

		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				parseJson(response);
			}

			@Override
			public void onFailure(Throwable e) {
				progressDialog.dismiss();
				Toast toast2 = Toast.makeText(SupplyToBuyActivity.this, "错误", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();

			}
		});
	}

	private void parseJson(JSONObject response) {
		try {
			JSONObject object = response;
			String succeed = object.getString("succeed");

			if (succeed.equals("000")) {
				Intent intent = new Intent(SupplyToBuyActivity.this, Index2Activity.class);
				startActivity(intent);
				Toast toast2 = Toast.makeText(SupplyToBuyActivity.this, "提交成功", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();

			} else {
				Toast toast2 = Toast.makeText(SupplyToBuyActivity.this, "无数据", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();
			}

			progressDialog.dismiss();

		} catch (JSONException e) {
			progressDialog.dismiss();
			e.printStackTrace();
		}
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == header_left) {
			goback();
		} else if (v == header_right) {
			if (app.user.getId() == "") {
				Intent intent = new Intent(SupplyToBuyActivity.this, LoginActivity.class);
				startActivity(intent);
			} else {
				progressDialog = ProgressDialog.show(SupplyToBuyActivity.this, null, "正在获取数据，请稍后.....", true, true);

				postSupplyOrderInfo();
			}
		}

	}

}